Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

crypto: expose new method OlmMachine::try_decrypt_room_event #4116

Merged
merged 4 commits into from
Oct 16, 2024

Conversation

richvdh
Copy link
Member

@richvdh richvdh commented Oct 10, 2024

... which returns an enum which encodes decryption failure information, rather than returning MegolmError

To make this work, we also extend the existing UnableToDecryptInfo to include a reason code for the failure.

Part of #4048

Copy link

codecov bot commented Oct 10, 2024

Codecov Report

Attention: Patch coverage is 62.50000% with 9 lines in your changes missing coverage. Please review.

Project coverage is 84.57%. Comparing base (a4bda1a) to head (f003f6b).
Report is 27 commits behind head on main.

Files with missing lines Patch % Lines
crates/matrix-sdk-crypto/src/machine/mod.rs 60.86% 9 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #4116      +/-   ##
==========================================
- Coverage   84.69%   84.57%   -0.13%     
==========================================
  Files         269      269              
  Lines       28816    28825       +9     
==========================================
- Hits        24405    24378      -27     
- Misses       4411     4447      +36     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@richvdh richvdh marked this pull request as ready for review October 15, 2024 16:57
@richvdh richvdh requested review from a team as code owners October 15, 2024 16:57
@richvdh richvdh requested review from bnjbvr and andybalaam and removed request for a team October 15, 2024 16:57
Copy link
Contributor

@andybalaam andybalaam left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me.

/// that session for some reason (e.g. incorrect MAC).
///
/// This represents all `vodozemac::megolm::DecryptionError`s, with the
/// exception of `UnknownMessageIndex`, which is represented as
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found this difficult to parse because of the special meaning of "exception" in my head. Would "with the exception of" -> "except" be better?

Copy link
Member

@bnjbvr bnjbvr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks for breaking it into simple changes :)

Comment on lines +637 to +639
fn unknown_utd_reason() -> UnableToDecryptReason {
UnableToDecryptReason::Unknown
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For what it's worth, you can instead derive(Default) to UnableToDecryptReason, and then mark one variant as the default:

#[default]
#[doc(hidden)]
Unknown,

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I wanted to explicitly avoid implementing Default for UnableToDecryptReason, because then people might start using it outside the deserialization case.

Comment on lines +670 to +673
MegolmDecryptionFailure,

/// The event could not be deserialized after decryption.
PayloadDeserializationFailure,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

double nit: since those are errors, i'd say "failure" in the variant's name is redundant, and would lightly encourage to remove it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't really agree. If I compare with something like MissingMegolmSession: a missing megolm session is a sensible reason for being unable to decrypt an event. So is a "megolm decryption failure". But just MegolmDecryption on its own doesn't really make sense to me.

I'm prepared to believe there could be better names for these things (UnableToDecryptMegolmMessage? VodozemacDecryptionError?), but I don't think MegolmDecryption would be a good one.

@@ -2540,6 +2536,38 @@ pub struct EncryptionSyncChanges<'a> {
pub next_batch_token: Option<String>,
}

fn megolm_error_to_utd_info(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you write a tiny doc comment for this, please? (It wasn't clear at a first glance at the call-site why this could return an error...)

Some(UnsignedDecryptionResult::UnableToDecrypt(UnableToDecryptInfo {
session_id,
}))
Err(e) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: can you name this variable err, if not error, please? One-letter variables are bleh.

) -> Result<RoomEventDecryptionResult, CryptoStoreError> {
match self.decrypt_room_event_inner(raw_event, room_id, true, decryption_settings).await {
Ok(decrypted) => Ok(RoomEventDecryptionResult::Decrypted(decrypted)),
Err(e) => Ok(RoomEventDecryptionResult::UnableToDecrypt(megolm_error_to_utd_info(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: e => err too here please

let decrypt_result =
bob.try_decrypt_room_event(&room_event, room_id, &decryption_settings).await.unwrap();
assert_let!(RoomEventDecryptionResult::UnableToDecrypt(utd_info) = decrypt_result);
assert!(utd_info.session_id.is_some());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could we assert the reason too?

@richvdh richvdh enabled auto-merge (rebase) October 16, 2024 15:30
@richvdh richvdh merged commit 87f89ec into main Oct 16, 2024
39 checks passed
@richvdh richvdh deleted the rav/try_decrypt_room_event branch October 16, 2024 15:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants